#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <unistd.h>
using namespace std;
#define LIBPATH "LD_PRELOAD=/usr/lib/libflealib.so.0.5.5 "
void showhelp(){
	cout<<"*******************************************************"<<endl;
	cout<<"*      NET FLEA v0.6.0 ,  author :mebetop@126.com     *"<<endl;
	cout<<"*******************************************************"<<endl;
	cout<<"    NAME"<<endl;
	cout<<"		netflea-nonui   --- the command line version of netflea"<<endl;
	cout<<"    USAGE"<<endl;
	cout<<"		netflea-nonui  [OPTIONS]... -e TARGET [TARGET_OPTIONS]..."<<endl;
	cout<<"    TARGET"<<endl;
	cout<<"		The program which will use proxy "<<endl;
	cout<<"    [TARGET_OPTIONS]"<<endl;
	cout<<"		Parameters for the TARGET of itself"<<endl;
	cout<<"    [OPTIONS]"<<endl;
	cout<<"    -m"<<endl;
	cout<<"	In which mode netflea will run.Available value for this "<<
	        "parameter are 'Normal','Detailing','Chain'.Take care that "<<
	        "the first character should be upper case. "<<endl;
	cout<<endl;
	///////////// /
	cout<<"    -t"<<endl;
	cout<<"	The value of connection timeout.Available value for this "<<
	        "parameter should be a integer "<<endl;
	cout<<endl;
   /////////////////
	cout<<"    -s"<<endl;
	cout<<"	Whether to show error message in the console.Available value for this "<<
	        "arameter should be 'y' or 'n' "<<endl;
	cout<<endl;
   /////////////////
	cout<<"    -h"<<endl;
	cout<<"	Show these directions"<<endl;
	cout<<endl;
	cout<<"************************************************"<<endl;
   /////////////////
	
}
int main(int argc,char *argv[]){
	 char **newargs;
    std::string proxytype = std::string("FLEATYPE=");
    std::string programnme = std::string("PROGRAM_NAME=");
    std::string timeoutvalue = std::string("CONNECTTIMEOUT=");
    std::string modeforshowerr=std::string("SHOWERRMODE=");

	if(argc==1){
		showhelp();
	}else if(argc==2){
		showhelp();
	}else if(argc==3){
		if(strcmp(argv[1],"-e")==0){
			char *args[2];
			newargs=args;
			args[0]=argv[2];
			args[1]=NULL;
			programnme=programnme+args[0];
		    putenv(LIBPATH);
		    putenv((char *)programnme.c_str());
		    if(execvp(newargs[0], &(newargs[0]))==-1){
		         exit(1);
		     }
			
		}else{
			showhelp();
		}
	}else{
		int i;
		for(i=1;i<argc;i++){
			if(strcmp(argv[i],"-e")==0){
				break;
			}
		}
		if(i<argc-1){
			int j=argc-i;
			char *args[j];
			newargs=args;
			for(int n=0;n<argc;n++){
			   args[n]=argv[i+1+n];
			}
			args[j]=NULL;
			programnme=programnme+argv[i+1];
			for(int m=1;m<i-1;m=m+2){
				if(strcmp(argv[m],"-t")==0){
					if(strcmp(timeoutvalue.c_str(),"CONNECTTIMEOUT=")==0){
						timeoutvalue=timeoutvalue+argv[m+1];
					}
				}else if(strcmp(argv[m],"-m")==0){
					if(strcmp(proxytype.c_str(),"FLEATYPE=")==0){
						proxytype=proxytype+argv[m+1];
					}
				}else if(strcmp(argv[m],"-s")==0){
					if(strcmp(modeforshowerr.c_str(),"SHOWERRMODE=")==0){
						modeforshowerr=modeforshowerr+argv[m+1];
					}
				}
			}
			//////////////////
			/////////////////
		    putenv(LIBPATH);
		    putenv((char *)proxytype.c_str());
		    putenv((char *)programnme.c_str());
		    putenv((char *)timeoutvalue.c_str());
		    putenv((char *)modeforshowerr.c_str());
		    if(execvp(newargs[0], &(newargs[0]))==-1){
		         exit(1);
		     }
			
		}else{
			showhelp();
		}
	}

}
